$(document).ready(function(){
	var param = window.dialogArguments;
	
	//초기화면 로딩
	layoutLoad("workList");
	
	//그룹 추가 탭 링크 세팅
	$("#work_tab > li").each(function( index ) {
		$(this).on("click", function(event){
			layoutLoad($(this).attr("id"));
		});
	});
	
	/* 예약작업,시스템선택 등록 */
	$(".btn_save").click(function(){
		var url = "";
		var param = {};
		var serviceName = "";//ajax serviceName
		
		$("#work_tab > li > a > img").each(function( index ) {
			if(index == 0){
				var src = $(this).attr("src");
				var onYn = src.substring(src.lastIndexOf("/")+1,src.lastIndexOf("."));
				
				/* 등록하려는것이 예약작업 인지 시스템선택 구분  */
				if(onYn == "lytab_work_1_on"){//예약작업
					//alert(onYn);
					url = "insertWorkAdd.jsp";
					serviceName = "WORK_ADD";
					param = {
							work_ex : $("#work_ex").val(),
							work_kind : $("#work_kind option:selected").val(),
							target_account : $("#target_account").val(),
							password : $("#password").val(),
							pw_check : $("#pw_check").val(),
							work_cycle : $("#work_cycle option:selected").val(),
							work_count : $("#work_count option:selected").val(),
							standard_time1 : $("#standard_time1").val()
					}
					
				}else if(onYn == "lytab_work_1"){//시스템선택
					url = "insertWorkAdd_tree.jsp";
					serviceName = "WORK_ADD_TREE";
					
				}else{
					url = "";
				}
			}
		});
		
		if(url != ""){
			callAjax(url,param, serviceName);
		}else{
			alert("등록실패");
		}
	});
	/* 팝업 닫기 */
	$(".btn_cancle").click(function(){
		window.close();
	});
	/* 팝업 닫기 */
	$(".btn_close").click(function(){
		window.close();
	});
});

function layoutLoad(id) {
	var url = "/jsp/admin/work/work_add_"+id+".jsp";
	$(".layer_cont").load(url, function() {
		$("#work_tab > li > a > img").each(function( index ) {
			var src = $(this).attr("src");
			var eachId = $(this).parent().parent().attr("id");
			var srcArray = new Array(3);
			srcArray[0] = src.substring(0,src.lastIndexOf("/"));
			srcArray[1] = src.substring(src.lastIndexOf("/")+1,src.lastIndexOf(".")).replace("_on","");
			srcArray[2] = src.substring(src.lastIndexOf(".")+1,src.length);
			
			if( eachId == id ) {
				srcArray[1] = srcArray[1] + "_on";
			}
			$(this).attr("src",srcArray[0]+"/"+srcArray[1]+"."+srcArray[2]);
		});
		
		if( id == "systemChoice" ) {
			loadTree("system");
		}
	});
}

/* callAjax successAjax */
function successAjax(data, serviceName){
	if(serviceName == "WORK_ADD"){
		alert(data.result);
	}else if(serviceName == "WORK_ADD_TREE"){
		
	}
}
